<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="dao.*,dao.impl.*,java.io.*" %>

<%
	String idStr = request.getParameter("id");
	int id = 0;
	if (idStr != null) {
		id = Integer.parseInt(idStr);
		//下载次数加一
		LearnResMagDao nmd = new LearnResMagImpl();
		boolean result = nmd.addDownCount(id);
	}	

	String filePath = request.getParameter("path");

	String fileName = "";

	if (filePath != null) {
		fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
	}
	
	response.setContentType("application/x-download");//设置为下载application/x-download
	response.addHeader("Content-Disposition", "attachment;filename="
			+ fileName);
			
    String filenamedownload = application.getRealPath("/") + filePath;
    
	OutputStream outp = null;
	FileInputStream in = null;
	try {
	
		outp = response.getOutputStream();
		in = new FileInputStream(filenamedownload);

		byte[] b = new byte[1024];
		int i = 0;

		while ((i = in.read(b)) > 0) {
			outp.write(b, 0, i);
		}
		outp.flush();
	} catch (Exception e) {
		System.out.println("Error!");
		e.printStackTrace();
	} finally {
		if (in != null) {
			in.close();
			in = null;
		}
		if (outp != null) {
			outp.close();
			outp = null;
		}
	}
%>

